package com.xjtu.platform.admin.dao;

import com.xjtu.platform.admin.dto.AddBookDto;
import com.xjtu.platform.admin.dto.DeleteBookDto;
import com.xjtu.platform.admin.dto.EditBookDto;
import com.xjtu.platform.admin.entity.AuthorList;
import com.xjtu.platform.common.entity.PrimaryKey;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * @program: platform
 * @description: 新增图书
 * @author: Wushanxu
 * @create: 2023-11-15 23:32
 **/
@Mapper
public interface AddBookDao {
    @Insert("insert into book " +
            "VALUES(null,#{dto.bookCode},#{dto.bookName},#{dto.isbn},#{dto.publicTime},#{dto.totalNum},#{dto.currentNum},'0',#{dto.info})")
    @Options(useGeneratedKeys = true, keyColumn = "book_id", keyProperty = "pk.id")
    void addNewBook(@Param("dto") AddBookDto addBookDto, @Param("pk") PrimaryKey primaryKey);

    @Insert("insert into book_class " +
            "VALUES(#{dto.classId},#{pk},null)")
    void addNewBookClass(@Param("dto") AddBookDto addBookDto, @Param("pk") String pkId);

    @Insert("insert into book_lable VALUES(#{dto.labelId},#{pk},null)")
    void addNewBookLable(@Param("dto") AddBookDto addBookDto, @Param("pk") String pkId);

    @Update("UPDATE book " +
            "SET book_code = #{dto.bookCode}, " +
            "    book_name = #{dto.bookName}, " +
            "    ISBN = #{dto.isbn}, publi_time = #{dto.publicTime}, total_num = #{dto.totalNum}, current_num = #{dto.currentNum}, total_score = #{dto.totalScore}, info = #{dto.info}" +
            "WHERE book_id = #{dto.bookId};")
    void editBook(@Param("dto") EditBookDto editBookDto, @Param("pk") PrimaryKey primaryKey);

    @Update("UPDATE author_book " +
            "SET author_id = #{dto.authorId} " +
            "WHERE book_id = #{dto.bookId};")
    void modifyAuthor(@Param("dto") EditBookDto editBookDto, @Param("pk") PrimaryKey primaryKey);

    @Update("UPDATE book_class " +
            "SET class_id = #{dto.classId} " +
            "WHERE book_id = #{dto.bookId};")
    void modifyClass(@Param("dto") EditBookDto editBookDto, @Param("pk") PrimaryKey primaryKey);

    @Update("UPDATE book_lable " +
            "SET label_id = #{dto.labelId} " +
            "WHERE book_id = #{dto.bookId};")
    void modifyLabel(@Param("dto") EditBookDto editBookDto, @Param("pk") PrimaryKey primaryKey);


    @Delete("DELETE FROM book_class WHERE book_id = #{dto.bookId}")
    void deleteBookClassByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM book_lable WHERE book_id = #{dto.bookId}")
    void deleteBookLableByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM book_pic WHERE book_id = #{dto.bookId}")
    void deleteBookPicByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM author_book WHERE book_id = #{dto.bookId}")
    void deleteAuthorBookByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM book_score_his WHERE book_id = #{dto.bookId}")
    void deleteBookScoreHisByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM book_reader_reserve_his WHERE book_id = #{dto.bookId}")
    void deleteBookReaderReserveHisByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM reader_book_borrow_his WHERE book_id = #{dto.bookId}")
    void deleteReaderBookBorrowHisByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Delete("DELETE FROM book WHERE book_id = #{dto.bookId}")
    void deleteBookByBookId(@Param("dto") DeleteBookDto deleteBookDto);

    @Insert("insert into author_book " +
            "VALUES(#{pk},#{dto.authorId},null)")
    void addNewBookAuthor(@Param("dto") AddBookDto addBookDto, @Param("pk") String pkId);

    @Insert("insert into file VALUES(null,#{filename},#{fileUrl})")
    @Options(useGeneratedKeys = true, keyColumn = "file_id", keyProperty = "pk.id")
    void addNewFile(@Param("filename") String filename, @Param("fileUrl") String fileUrl, @Param("pk") PrimaryKey primaryKey);

    @Insert("insert into book_pic VALUES(#{fileId},#{bookId},null)")
    void addNewBookFile(@Param("bookId") String bookId, @Param("fileId") String fileId);

    @Delete("DELETE FROM book_pic WHERE book_id = #{bookId}")
    void deleteBookFile(@Param("bookId") String bookId);

    @Select("select * from author")
    List<AuthorList> getAuthorList();
}
